package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Ticket;
import cn.wolfcode.luowowo.article.domain.TicketDetail;
import cn.wolfcode.luowowo.article.mapper.TicketDetailMapper;
import cn.wolfcode.luowowo.article.mapper.TicketMapper;
import cn.wolfcode.luowowo.article.service.ITicketService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class TicketServiceImpl implements ITicketService {
    @Autowired
    private TicketMapper ticketMapper;
    @Autowired
    private TicketDetailMapper ticketDetailMapper;

    @Override
    public List <Ticket> list() {
        List <Ticket> tickets = ticketMapper.selectAll();
        return tickets;
    }

    @Override
    public BigDecimal selectMinPrice(Long tid) {

        BigDecimal minPrice = ticketDetailMapper.selectMinPrice( tid );
        return minPrice;
    }

    @Override
    public Date selectEarlyAccessTime(Long id) {
        List <Date> ts = ticketDetailMapper.selectEarlyAccessTime( id );
        return ts.get( 0 );
    }

    @Override
    public Ticket selectbyId(Long id) {
        Ticket ticket = ticketMapper.selectByPrimaryKey( id );
        BigDecimal minPrice = ticketDetailMapper.selectMinPrice( id );
        BigDecimal maxPrice = ticketDetailMapper.selectMaxPrice( id );

        ticket.setMinPrice( minPrice );
        ticket.setMaxPrice( maxPrice );
        return ticket;
    }

    @Override
    public List <TicketDetail> selectBydestIdAndTicketlId(Long destId, Long tid) {
        List <TicketDetail> ts = ticketDetailMapper.selectBydestIdAndTicketlId( destId, tid );

        return ts;


    }

    @Override
    public TicketDetail selectTicketId(String tid) {

        TicketDetail ticketDetail = ticketDetailMapper.selectByloscId( tid );
        return ticketDetail;
    }

    @Override
    public TicketDetail selectByPno(String pno) {
        TicketDetail detail = ticketDetailMapper.selectByloscId( pno );
        return detail;
    }

    @Override
    public List <Ticket> selectBydestId(Long destId) {
        List <Ticket> list = ticketMapper.selectBydestId( destId );
        getStatis( list );

        return list;
    }

    @Override
    public List <Ticket> selectCommendTicketsTop4() {
        List <Ticket> list = ticketMapper.selectCommendTicketsTop4();
        getStatis( list );
        return list;
    }

    @Override
    public List <Ticket> selectBydestIdAndThemelId(Long ajaxDestId, Long ajaxSubjectId) {
        List <Ticket> tickets = ticketMapper.selectBydestIdAndThemelId( ajaxDestId, ajaxSubjectId );
        getStatis( tickets );
        return tickets;

    }

    void getStatis(List <Ticket> list) {
        for (Ticket ticket : list) {
            BigDecimal minPrice = ticketDetailMapper.selectMinPrice( ticket.getId() );
            BigDecimal maxPrice = ticketDetailMapper.selectMaxPrice( ticket.getId() );

            ticket.setMinPrice( minPrice );
            ticket.setMaxPrice( maxPrice );
        }
    }

}
